﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Net;
using System.IO;

namespace Hbrs.Soa.Restclient.Prototype
{
    class BookServiceClient : ICollectionService<Book>
    {
        #region ICollectionService<Book> Members

        public void AddItemInXml(Book initialValue)
        {
            WebRequest request = WebRequest.Create("http://localhost:1271/Service.svc/");
            request.Method = "POST";
            request.ContentType = "application/xml";
            Stream s = request.GetRequestStream();
            XmlSerializer xmlser = new XmlSerializer(typeof(Book));
            xmlser.Serialize(s, initialValue);
            s.Flush();
            s.Close();
            request.GetResponse();
        }

        public void DeleteItem(string id)
        {
            throw new NotImplementedException();
        }

        public Book GetItemInXml(string id)
        {
            throw new NotImplementedException();
        }

        public List<Book> GetItemsInXml()
        {
            WebRequest request = WebRequest.Create("http://localhost:1271/Service.svc/");
            WebResponse resp = request.GetResponse();
            Stream responseStream = resp.GetResponseStream();

            XmlSerializer xmlser = new XmlSerializer(typeof(ItemInfoList));
            ItemInfoList list = (ItemInfoList)xmlser.Deserialize(responseStream);
            List<Book> bookList = (from ItemInfoListItemInfo b in list.ItemInfo select new Book() { ISBN = b.Item.ISBN, Title = b.Item.Title }).ToList();
            return bookList;
        }

        public Book UpdateItemInXml(string id, Book newValue)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
